package com.lsl.gencode.web;

import com.alibaba.fastjson.JSONObject;
import com.lsl.db.Datasource;
import com.lsl.easycode.common.DBType;
import com.lsl.easycode.core.DbQuery;
import com.lsl.easycode.language.I18N;
import com.lsl.easycode.utils.JdbcProperty;
import com.lsl.easycode.vo.Result;
import org.beetl.ext.servlet.ServletGroupTemplate;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by liusl on 2016/12/26.
 */
public class ConfigController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        request.setAttribute(I18N.DRIVER.getKey(), JdbcProperty.getValue(I18N.DRIVER.getKey()));
        request.setAttribute(I18N.PORT.getKey(), JdbcProperty.getValue(I18N.PORT.getKey()));
        request.setAttribute(I18N.HOST.getKey(), JdbcProperty.getValue(I18N.HOST.getKey()));
        request.setAttribute(I18N.USERNAME.getKey(), JdbcProperty.getValue(I18N.USERNAME.getKey()));
        request.setAttribute(I18N.PASSWORD.getKey(), JdbcProperty.getValue(I18N.PASSWORD.getKey()));
        request.setAttribute(I18N.DATABASE.getKey(), JdbcProperty.getValue(I18N.DATABASE.getKey()));
        ServletGroupTemplate.instance().render("/config.html", request, response);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader("content-type", "text/html;charset=UTF-8");
        Result result = null;
        if ("save".equalsIgnoreCase(req.getParameter("direct"))) {
            result = save(req, resp);
        }
        if ("test".equalsIgnoreCase(req.getParameter("direct"))) {
            result = test(req, resp);
        }
        resp.getWriter().write(JSONObject.toJSONString(result));
        resp.getWriter().flush();
        resp.getWriter().close();

    }

    private Result save(HttpServletRequest req, HttpServletResponse resp) {
        Result result = new Result();
        try {
            String driver = req.getParameter(I18N.DRIVER.getKey());
            String port = req.getParameter(I18N.PORT.getKey());
            String host = req.getParameter(I18N.HOST.getKey());
            String username = req.getParameter(I18N.USERNAME.getKey());
            String password = req.getParameter(I18N.PASSWORD.getKey());
            String database = req.getParameter(I18N.DATABASE.getKey());
            JdbcProperty.setValue(I18N.DRIVER.getKey(), driver);
            JdbcProperty.setValue(I18N.PORT.getKey(), port);
            JdbcProperty.setValue(I18N.HOST.getKey(), host);
            JdbcProperty.setValue(I18N.USERNAME.getKey(), username);
            JdbcProperty.setValue(I18N.PASSWORD.getKey(), password);
            JdbcProperty.setValue(I18N.DATABASE.getKey(), database);
            JdbcProperty.save();
            result.setSuccess(true);
            result.setTips(I18N.TIP_SAVE_SUCC.getDesc());

        } catch (Exception e) {
            result.setSuccess(true);
            result.setTips(I18N.TIP_SAVE_FAILD.getDesc());
        }
        return result;
    }


    private Result test(HttpServletRequest req, HttpServletResponse resp) {
        Result result = null;
        try {
            new DbQuery().test();
            result = new Result().newSucc(I18N.TIP_CONN_SUCC.getDesc());
        } catch (Exception e) {
            result = new Result().newFaild(I18N.ERROR_DB.getDesc());
        }
        return result;
    }
}
